﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain.Membership;
using Ue.Qcdz.Domain.Roles;

namespace Ue.Qcdz.UI.Controls.Membership
{
    public class UserRole : SkinnedControl
    {
        private Label lblUserName;
        private CheckBoxList chkRole;
        private string skinFileName = "~/Admin/Themes/Skins/Membership/UserRoleEdit.ascx";

        private Button btnConfirm;
        #region Overrides of SkinnedControl
        private void BindRole()
        {

        }

        protected override string GetSkinFileName()
        {
            return skinFileName;
        }

        protected override void InitializeSkin(Control skin)
        {
            string strUserId = this.Page.Request["id"];
            if (!string.IsNullOrEmpty(strUserId))
            {
                lblUserName = skin.FindControl<Label>("lblUserName");
                chkRole = skin.FindControl<CheckBoxList>("chkRole");
                btnConfirm = skin.FindControl<Button>("btnConfirm");
                btnConfirm.Click += new EventHandler(btnConfirm_Click);
                Guid id = new Guid(strUserId);
                IRoleRepository roleRepository =
                   Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IRoleRepository, Role>();
                IUserRepository userRepository =
                    Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
                User user = userRepository.FindBy(id);

                IList<Role> roleList = roleRepository.FindAll();
                foreach (var item in roleList.Select(role => new ListItem(role.Name, role.Key.ToString())))
                {
                    chkRole.Items.Add(item);
                }
                lblUserName.Text = user.UserName;
                if (user.Roles.Count > 0)
                {
                    BindRoleCheck(user);
                }
            }
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            IRoleRepository roleRepository =
                   Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IRoleRepository, Role>();
            List<Role> roleList = new List<Role>();
            for (int i = 0; i < chkRole.Items.Count; i++)
            {
                if (chkRole.Items[i].Selected)
                {
                    Guid roleId = new Guid(chkRole.Items[i].Value);
                    Role role = new Role(roleId);
                    roleList.Add(role);
                }
            }
            if (roleList.Count > 0)
                roleRepository.AddInUserRole(roleList, this.Page.Request["id"]);
            MessageWindow.RenderMessage("修改成功");

        }
        /// <summary>
        /// 把User中存在的Role,在CheckBox中选中
        /// </summary>
        /// <param name="user"></param>
        private void BindRoleCheck(User user)
        {
            foreach (var role in user.Roles)
            {
                for (int i = 0; i < chkRole.Items.Count; i++)
                {
                    if (chkRole.Items[i].Value.Equals(role.Key.ToString()))
                    {
                        chkRole.Items[i].Selected = true;
                    }
                }
            }
        }



        #endregion
    }
}
